package com.xtayfjpk.zookeeper.test.master_workers.curator.listener;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;

import com.xtayfjpk.zookeeper.test.master_workers.curator.CuratorMaster;

public class TasksCacheListener implements PathChildrenCacheListener {
	private CuratorMaster curatorMaster;
	
	public TasksCacheListener(CuratorMaster curatorMaster) {
		this.curatorMaster = curatorMaster;
	}

	@Override
	public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
		if(event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
			curatorMaster.assignTask(event.getData().getPath().replaceFirst("/tasks/", ""),
                        event.getData().getData());
        }
	}

}
